package com.ice.work.moment.crawler.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.honeybee.cloud.framework.base.exception.business.BaseBizException;
import com.honeybee.cloud.framework.common.constant.CommonSystemConstant;
import com.honeybee.cloud.framework.common.enums.DataStatusEnum;
import com.honeybee.cloud.framework.common.page.Page;
import com.ice.work.moment.crawler.dao.share.ShareRepository;
import com.ice.work.moment.crawler.service.IShareService;
import com.ice.work.moment.facade.crawler.dto.ShareDTO;
import com.ice.work.moment.facade.crawler.mapper.ShareConvertMapper;
import com.ice.work.moment.facade.crawler.model.Share;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Collection;
import java.util.List;

/**
 * @Description
 * @Author DELL
 * @Create 2019-05-05
 * @Since 1.0.0
 */
@Slf4j
@Service
public class ShareServiceImpl implements IShareService {
    @Autowired
    private ShareRepository shareRepository;

    @Override
    public Boolean saveOrUpdate(Collection<Share> shares) throws BaseBizException {
        Assert.notEmpty(shares, "shares not empty");
        return shareRepository.saveOrUpdateBatch(shares, CommonSystemConstant.BATCH_SIZE);
    }

    @Override

    public ShareDTO findByShareCode(String shareCode) throws BaseBizException {
        Assert.hasLength(shareCode, "shareCode not null");
        Share share = shareRepository.findByShareCodeAndDataStatus(shareCode, DataStatusEnum.VALID.key);
        return ShareConvertMapper.INSTANCE.toDTO(share);
    }

    @Override
    public List<ShareDTO> findByShareCodes(Collection<String> shareCodes) {
        Assert.notEmpty(shareCodes, "shareCodes not null");
        List<Share> shares = shareRepository.findByShareCodeInAndDataStatus(shareCodes, DataStatusEnum.VALID.key);
        return ShareConvertMapper.INSTANCE.toDTO(shares);
    }

    @Override
    public Page<ShareDTO> page(IPage<Share> page) {
        IPage<Share> resourceIPage = shareRepository.page(page);
        return ShareConvertMapper.INSTANCE.toDTO(resourceIPage);
    }

    @Override
    public ShareDTO findById(Long id) throws BaseBizException {
        Assert.notNull(id, "id not null");
        Share share = shareRepository.findByIdAndDataStatus(id,DataStatusEnum.VALID.key);
        return ShareConvertMapper.INSTANCE.toDTO(share);
    }
}
